package com.controller;

import com.dao.UserDao;
import com.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class UserController {
/*
     向用户登陆界面跳转
   */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
/*    public String toLogin(){
        return "/index.jsp";
    }*/
/*
    用户登陆

    */
    public String login(User user, Model model, HttpSession session) throws Exception {
        try {
            //获取用户名和密码
            UserDao find = new UserDao();
            User a = find.findUserForLogin(user);
            if (a != null) {
                //将用户添加到Session
                session.setAttribute("USER_SESSION", a);
                //重定向到主页面的跳转方法
                return "/vetsearch.jsp";
            }
            model.addAttribute("msg", "用户名或密码错误，请重新登陆！");
            return "/index.jsp";
        } catch (Exception e) {
            model.addAttribute("msg", e.getMessage());
            return "customeradd.jsp";
        }
    }
/*
    向用户主页面跳转
    */
/*    public String toMain(){
        return "/vetsearch.jsp";
    }*/
/*
    退出登陆
    */
/*    public String logout(HttpSession session){
        //清除session
        session.invalidate();
        //重定向到登陆页面的跳转方法
        return "redirect:login";
    }*/

/*
    添加客户
    */
    @RequestMapping(value = "/doAdd")
    public String doAdd(User user, Model model)throws Exception{
       try {
           UserDao U = new UserDao();
           U.addUserTest(user);
           model.addAttribute("msg","添加客户成功");
           return "/customeradd.jsp";
       } catch (Exception e){
           model.addAttribute("msg",e.getMessage());
           return "customeradd.jsp";
       }
    }
 /*
    删除客户
    */

    @RequestMapping(value = "/doDel")
    public String doDel(User id,Model model)throws Exception {
        UserDao Delete = new UserDao();
        Delete.deleteUserTest(id);
        model.addAttribute("msg","删除客户成功");
        return "/customersearch.jsp";
    }
/*
    查询客户
    */
    @RequestMapping(value = "/doFindName", method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    public String doFindName(String name,Model model)throws Exception {
        UserDao my = new UserDao();
        List<User> users = my.findUserByNameTest(name);
        if (0 == users.size()){
            model.addAttribute("msg","没有找到客户信息");
            return "/customersearch.jsp";
        }else {

            model.addAttribute("cs", users);
            return "/customersearch_result.jsp";
        }
    }
/*
查询所有客户用于添加宠物
    */
    @RequestMapping(value = "/doFindAllCustomer")
    public String doFindAllCustomer(String role,Model model)throws Exception {
        UserDao my = new UserDao();
        role="customer";
        List<User> users = my.findUserForAddPet(role);
        if (0 == users.size()){
            model.addAttribute("msg","没有找到客户信息");
            return "/newPetAdd.jsp";
        }else {

            model.addAttribute("cs", users);
            return "/newPetAdd.jsp";
        }
    }
}
